/**
 * 
 */
package pl.spikes.xmpp;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;

import pl.spikes.JsonSpikeActivity;
import pl.spikes.R;
import pl.spikes.AsynctaskSpikeActivity.BackgroundAsyncTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @author smimpin02
 *
 */
public class XMPPLoginActivity extends Activity implements OnClickListener {
	EditText user_name_input,password_input;
	TextView error_message;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.xmpp_login);
		View login_button = this.findViewById(R.id.login_button);
		user_name_input = (EditText)findViewById(R.id.user_name_input);
		password_input = (EditText)findViewById(R.id.password_input);
		error_message = (TextView)findViewById(R.id.error_message);
		login_button.setOnClickListener(this);
	}

	private void openConnection(String username, String password) throws XMPPException {
		System.out.println("XMPPSpikeActivity openconnection");
    	ConnectionConfiguration config = new ConnectionConfiguration("vps01.fit3140hack.org",5222);
    	config.setSASLAuthenticationEnabled(true);
        XMPPConnection conn = new XMPPConnection(config);
        XMPPLogic.getInstance().setConnection(conn);
        XMPPLogic.getInstance().getConnection().connect();
        XMPPLogic.getInstance().getConnection().login(username, password);
    	
    }
	
	private void openConnection() throws XMPPException {
		System.out.println("XMPPSpikeActivity openconnection");
    	ConnectionConfiguration config = new ConnectionConfiguration("vps01.fit3140hack.org",5222);
    	config.setSASLAuthenticationEnabled(true);
        XMPPConnection conn = new XMPPConnection(config);
//        XMPPLogic.getInstance().setConnection(conn);
//        XMPPLogic.getInstance().getConnection().connect();
//        XMPPLogic.getInstance().getConnection().login(username, password);
//        conn.connect();
//        conn.login("leoli", "890223");
    	
    }

	
	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()) {
			case R.id.login_button:
				String username = user_name_input.getText().toString();
				String password = password_input.getText().toString();
				System.out.println(username);
				System.out.println(password);
			try {
				openConnection(username, password);
//				openConnection();
				System.out.println("connection success");
				// go to next srceen
				Intent intent = new Intent(this, XMPPMultiplayerActivity.class);
				startActivity(intent);
			} catch (XMPPException e) {
				// TODO Auto-generated catch block
				Log.d("XMPPSpikeActivity", "exception caught:" + e.getMessage());
				e.printStackTrace();
				error_message.setText("login failed! please try again");
			}		
				break;
			default:
		}

	}

}
